package com.ucar.train.service.impl;

import com.github.pagehelper.PageHelper;
import com.ucar.train.dao.CrmCommentMappingMapper;
import com.ucar.train.dao.CrmMapper;
import com.ucar.train.dao.OrderDetailMapper;
import com.ucar.train.dao.OrderMapper;
import com.ucar.train.dto.*;
import com.ucar.train.service.CarService;
import com.ucar.train.service.CrmAttentionService;
import com.ucar.train.service.OrderDetailService;
import com.ucar.train.service.OrderService;
import com.ucar.train.utils.JodaUtils;
import com.ucar.train.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhoujinmu
 * @title StoreAttentionServiceImpl
 * @projectName train-parent
 * @description TODO
 * @created 2019-08-27 13:52
 **/
@Service
public class CrmAttentionServiceImpl implements CrmAttentionService {

    @Autowired
    private CrmCommentMappingMapper crmCommentMappingMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private CrmMapper crmMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Autowired
    private OrderService orderService;


    @Autowired
    private CarService carService;

    @Override
    public void addCrmAttention(CrmCommentMappingDTO crmCommentMappingDTO) {
        crmCommentMappingDTO.setCreateTime(JodaUtils.nowAsDate());
        crmCommentMappingDTO.setModifyTime(JodaUtils.nowAsDate());
        crmCommentMappingMapper.insert(crmCommentMappingDTO);
    }

    @Override
    public List<OrderDTO> getAttentionStoreComment(Long cid, Integer page, Integer size) {
//        PageHelper.startPage(page,size);
        List<OrderDTO> orderDTOS = new ArrayList<>();
        List<Long> attentionCrmId = crmCommentMappingMapper.getAttentionCrmIdByCrmId(cid);
        for (Long crmId : attentionCrmId) {
            System.out.println(crmId);
            orderDTOS.addAll(orderMapper.getCommentByCustomerID(crmId));
        }
//        System.out.println(orderDTOS);
//        System.out.println(orderDetailMapper.getCommentAndStoreByOrderNo("CNO1970010100000000001"));
        return orderDTOS;
    }

    @Override
    public List<CrmDTO> getAllCustomer() {
        return crmMapper.getAll();
    }


    /**
     * @param
     * @Description 获取关注的用户
     * @author LiuHao
     * @createDate 2019/8/28
     **/
    public List<CrmDTO> getFollowed(Long crmID) {
        List<Long> FollowedIDs = crmCommentMappingMapper.getAttentionCrmIdByCrmId(crmID);
        List<CrmDTO> Followed = new ArrayList<>();
        for (Long ID : FollowedIDs) {
            Followed.add(crmMapper.selectByCustomerID(ID));
        }
        return Followed;
    }


    /**
     * @param
     * @Description 获取用户租用过的车辆
     * @author LiuHao
     * @createDate 2019/8/28
     **/
    public List<OrderVO> getUesdCar(Long crmID) {
        //查询出这个用户所有的订单
        List<OrderDTO> orderDTOList = orderService.SelectOrdersByCustomerId(crmID);
        List<OrderVO> orderVOList = new ArrayList<>();
        for (OrderDTO order : orderDTOList) {
            OrderVO orderVO;
            //根据订单号查找车辆id
            OrderDetailDTO orderDetailDTO = orderDetailMapper.selectByOrderNo(order.getOrderNo());
            //根据车辆id查找车辆类型
            CarDTO carDTO= carService.selectCarDTOByCarId(orderDetailDTO.getCarId());

            orderVO = assembleOrderVO(order, orderDetailDTO, carDTO);
            orderVOList.add(orderVO);
        }
        return orderVOList;
    }

    public OrderVO assembleOrderVO(OrderDTO orderDTO, OrderDetailDTO orderDetailDTO, CarDTO carDTO) {

        OrderVO orderVO = new OrderVO();

        orderVO.setOrderNo(orderDTO.getOrderNo());
        orderVO.setCarNumber(orderDTO.getCarNumber());

        //获取车辆评论
        orderVO.setCarComment(orderDetailDTO.getCarComment());

        orderVO.setType(carDTO.getType());
        return orderVO;
    }
}
